<?php
namespace plugins\appmarket\controller; 

use cmf\controller\PluginAdminBaseController;//引入此类
use think\Db;
use plugins\appmarket\common\Common;
use app\common\lib\Upload;


//AdminIndexController类和类的index()方法是必须存在的 index() 指向admin_index.html模板也就是模块后台首页
// 并且继承PluginAdminBaseController
class TemplateFrameController extends PluginAdminBaseController
{
    /**
     * 文件存储根目录
     * @var string
     */
    private $rootPath = "upload/templateFramework";

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    /**
     * @adminMenu(
     *     'name'   => '框架版本配置',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '框架版本配置',
     *     'param'  => ''
     * )
     */
    public function frame()
    {
        $data = Db::name('module_temp_framework')->order('id','asc')->paginate(20);

        $this->assign( 'data' , $data );
        return $this->fetch( 'template/frame_index' );
    }


    /**
     * 框架版本删除
     * @adminMenu(
     *     'name'   => '框架版本删除',
     *     'parent' => 'frame',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '框架版本删除',
     *     'param'  => ''
     * )
     */
    public function frameDel(){
        $param = $this->request->param();
        if(empty($param['id'])){
            $this->error("传参错误");
        }
        $id = $param['id'];
        $re = Db::name('module_temp_framework')->where('id',$id)->delete();
        if(empty($re)){
            $this->error("删除失败");
        }
        $this->success("删除成功");
    }

    /**
     * 框架版本添加
     * @return [type] [description]
     */
    public function frameAdd(){
        if($this->request->isPost()){
            $request = $this->request->post();
			//dump($request);exit;
            if(empty($request['name']) || empty($request['src']) || empty($request['describe']) || empty($request['type'])){
                return zy_json_echo(false,'添加失败1！','',-1);
            }

            $data = Db::name('module_temp_framework')->order('id','desc')->find();
            if($request['type']==1){
                $request['versions']=$data['versions']+0.1;
            }else{
                $request['versions']=(intval($data['versions'])+1).'.0';
            }
			
			$src = str_replace( '//' ,'/' , $request['src']);
			$src = str_replace( '\\\\' ,'\\' , $request['src']);
            $arr = [
                'name'=>$request['name'],
                'src'=>$src,
                'add_time'=>time(),
                'user_id'=>cmf_get_current_admin_id(),
                'user_name'=>Db::name('user')->where('id' , cmf_get_current_admin_id())->value('user_login'),
                'versions'=>$request['versions'],
                'describe'=>$request['describe']
            ];
            $result = Db::name('module_temp_framework')->insert($arr);
            return zy_json_echo(true,'添加成功！','',200);
        }else{
            $data = Db::name('module_temp_framework')->order('id','desc')->find();
            $this->assign( 'data' , $data );
            return $this->fetch( 'template/frame_add' );
        }
    }


    /**
     * 文件上传
     * @return [type] [description]
     */
    public function fileUpload(){
        $data = Db::name('module_temp_framework')->order('id','desc')->find();

        $path = '';
        return $this->uploadFile ( $path , 500 , ['zip','rar'] );
    }


    /**
     * 上传文件公共方法
     */
    private function uploadFile ( $path = '' , $size = 10 , $exts = ['zip'] , $fileName = '' )
    {
        try {
            $upload = new Upload();
            $upload->setSize( $size );
            if( !empty( $exts ) ){
                $upload->exts = $exts;
            }
            if( !empty( $fileName ) ){
                $upload->fileName = $fileName;
            }
            $upload->rootPath = $this->rootPath.DS.$path;
            $upload->setResourceInfo( $_FILES );
            return $upload->uploadFile();
        } catch (\Exception $e) {
            return zy_json_echo( false , $e->getMessage() );
        }
    }

}